package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    MemberDao memberDao;

    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberDao.add(member);
    }

    /**
     * 需要的数据
     *      months: [2018.10,.....,2019.9]
     *      memberCount: [100,.....,1000]
     * @return
     */
    @Override
    public Map<String, Object> getMemberReport() {

        //获取最近的12个月
        List<String> months = new ArrayList<>();
        List<Long> memberCount = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -11);

        for (int i = 0; i < 12; i++) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String str = simpleDateFormat.format(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
            months.add(str);
        }
        //memberCount:每个会员数量
        for (String date : months) {
            // 2018-10-31
            date += "-31";
            long count = memberDao.findByBeforeDate(date);
            memberCount.add(count);
        }

        //组合数据
        Map<String,Object> map = new HashMap<>();
        map.put("months",months);
        map.put("memberCount",memberCount);

        return map;
    }

    @Override
    public List<Map<String, Object>> getMemberAllCount() {
        return memberDao.getMemberAllCount();
    }

    @Override
    public List<Map<String, Object>> getMemberAgeCount() {
        return memberDao.getMemberAgeCount();
    }






    @Override               //默认显示
    public Map<String, Object> findByDate() {
        ArrayList<String> monthss = new ArrayList<>();
        ArrayList<Long> memberCount = new ArrayList<>();
        //默认获取前12个月
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-11);
        for(int i = 0; i<12; i++){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            String str = simpleDateFormat.format(calendar.getTime());
            calendar.add(Calendar.MONTH, 1);
            monthss.add(str);
        }
        //获取每个月的会员数量
        for (String date : monthss) {
            date+="31";
            long byDate = memberDao.findByDate(date);
            memberCount.add(byDate);
        }
        //组合数据
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("months",monthss);
        hashMap.put("memberCount",memberCount);
        return hashMap;
    }

    @Override       //自定义显示
    public Map<String, Object>  findByDateZdy(Map<String, String> map) throws ParseException {
        ArrayList<String> monthss = new ArrayList<>();
        ArrayList<Long> memberCount = new ArrayList<>();
        //获取时间字符串并转换成date类型
        String orderDate1 = map.get("orderDate1");
        String orderDate2 = map.get("orderDate2");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            Date parse1 = sdf1.parse(orderDate1);
            Date parse2 = sdf1.parse(orderDate2);
            c1.setTime(parse1);
            c2.setTime(parse2);
            //分别获取年/月
            int year1 = c1.get(Calendar.YEAR);
            int year2 = c2.get(Calendar.YEAR);
            int month1 = c1.get(Calendar.MONTH)+1;
            int month2 = c2.get(Calendar.MONTH)+1;
            int yearInterval = year1 - year2;
            //根据差值判断
            //时间1 大于 时间2
            if(yearInterval>0){
                return null;
            }
            //时间1 等于 时间2
            else if(yearInterval==0){
                //判断月份
                //时间1 不大于 时间2
                if(month1<=month2){
                    //将时间段储存到集合
                    for (int j = month1; j <=month2 ; j++) {
                        String str = year1+"-"+j;
                        monthss.add(str);
                    }
                }else{
                    return null;
                }
            }//时间1 小于 时间2
            else{
                int g = 12;
                for (int i = month1; i <=g; i++) {
                    String str = year1+"-"+i;
                    monthss.add(str);
                    if(i==12){
                        i=1;
                        year1+=1;
                        if(++yearInterval==0){
                            g=month2;
                        }
                    }
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        //遍历集合查询会员人数
        for (String date : monthss) {
            date+="-31";
            long byDate = memberDao.findByDate(date);
            memberCount.add(byDate);
        }
        //组合数据
        Map<String,Object> hashMap = new HashMap<>();
        hashMap.put("months",monthss);
        hashMap.put("memberCount",memberCount);
        return hashMap;

    }







}
